package com.ruoyi.system.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ProjectConstant;
import com.ruoyi.common.utils.shormsg.SmsSendResult;
import com.ruoyi.common.utils.shormsg.SmsUtil;
import com.ruoyi.system.domain.SmsRecord;
import com.ruoyi.system.mapper.SmsRecordMapper;
import com.ruoyi.system.service.ISmsRecordService;

/**
 * 短信发送记录Service业务层处理
 * 
 * @author DEREK
 * @date 2023-05-22
 */
@Service
public class SmsRecordServiceImpl extends ServiceImpl<SmsRecordMapper, SmsRecord> implements ISmsRecordService 
{
    @Autowired
    private SmsRecordMapper smsRecordMapper;
    @Autowired
	private RedisUtil redisUtil;
    
    /**
     * 查询短信发送记录
     * 
     * @param id 短信发送记录主键
     * @return 短信发送记录
     */
    @Override
    public SmsRecord selectSmsRecordById(Long id)
    {
        return smsRecordMapper.selectSmsRecordById(id);
    }

    /**
     * 查询短信发送记录列表
     * 
     * @param smsRecord 短信发送记录
     * @return 短信发送记录
     */
    @Override
    public List<SmsRecord> selectSmsRecordList(SmsRecord smsRecord)
    {
        return smsRecordMapper.selectSmsRecordList(smsRecord);
    }

    /**
     * 新增短信发送记录
     * 
     * @param smsRecord 短信发送记录
     * @return 结果
     */
    @Override
    public int insertSmsRecord(SmsRecord smsRecord)
    {
        smsRecord.setCreateTime(DateUtils.getNowDate());
        return smsRecordMapper.insertSmsRecord(smsRecord);
    }

    /**
     * 修改短信发送记录
     * 
     * @param smsRecord 短信发送记录
     * @return 结果
     */
    @Override
    public int updateSmsRecord(SmsRecord smsRecord)
    {
        smsRecord.setUpdateTime(DateUtils.getNowDate());
        return smsRecordMapper.updateSmsRecord(smsRecord);
    }

    /**
     * 批量删除短信发送记录
     * 
     * @param ids 需要删除的短信发送记录主键
     * @return 结果
     */
    @Override
    public int deleteSmsRecordByIds(Long[] ids)
    {
        return smsRecordMapper.deleteSmsRecordByIds(ids);
    }

    /**
     * 删除短信发送记录信息
     * 
     * @param id 短信发送记录主键
     * @return 结果
     */
    @Override
    public int deleteSmsRecordById(Long id)
    {
        return smsRecordMapper.deleteSmsRecordById(id);
    }

	@Override
	public AjaxResult sendSms(Integer type, String mobile) {
		String code = (int) ((Math.random() * 9 + 1) * 100000) + "";
		SmsSendResult smsSendResult = SmsUtil.sendSms(mobile, code);
		SmsRecord smsRecord = new SmsRecord();
		smsRecord.setContent(code);
		smsRecord.setPhone(mobile);
		smsRecord.setStatus(ProjectConstant.COMMON_STATUS_NOT_AVAILABLE);
		smsRecord.setType(type);
		smsRecord.setBizId(smsSendResult.getBizId());
		smsRecord.setRequestId(smsSendResult.getRequestId());
		smsRecord.setRemark(smsSendResult.getCode());
		smsRecord.setSuccess(ProjectConstant.COMMON_STATUS_AVAILABLE);
		if (smsSendResult.isSuccess()) {
			redisUtil.set(ProjectConstant.RedisKey.MOBILE_CODE.getKeyPrefix() +"_"+ type + "_" + code,mobile,ProjectConstant.RedisKey.MOBILE_CODE.getExpire());
			smsRecord.setStatus(ProjectConstant.COMMON_STATUS_AVAILABLE);
			this.save(smsRecord);
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("mobile",mobile);
//			jsonObject.put("code",code);
			return AjaxResult.success("发送验证码成功",jsonObject);
		}
		smsRecord.setSuccess(ProjectConstant.COMMON_STATUS_NOT_AVAILABLE);
		smsRecord.setRemark(smsSendResult.getMessage());
		this.save(smsRecord);
		JSONObject resultJson = new JSONObject();
		resultJson.put("mobile",mobile);
		if (smsSendResult.getCode().equals("isv.BUSINESS_LIMIT_CONTROL")) {
			String[] arr = smsSendResult.getMessage().split(":");
			if (arr.length > 1) {

				if (arr[1].equals("10")) {
					return new AjaxResult(100002,"一个手机号码一天内只能发送10条短信", resultJson);
				}
				if (arr[1].equals("5")) {
					return new AjaxResult(100003,"一个手机号码一小时内只能发送5条短信", resultJson);
				}
				if (arr[1].equals("1")) {
					return new AjaxResult(100004,"一个手机号码分钟内只能发送1条短信", resultJson);
				}
			}
		}
		return new AjaxResult(100005,"短信验证码发送失败",resultJson);
		
//		JSONObject startSendCode = SendCode.startSendCode(mobile,code);
//		code = startSendCode.getString("obj");
//		SmsRecord smsRecord = new SmsRecord();
//		smsRecord.setContent("验证码为："+code);
//		smsRecord.setPhone(mobile);
//		smsRecord.setStatus(0);
//		smsRecord.setType(type);
//		smsRecord.setRemark(startSendCode.toJSONString());
//		smsRecord.setSuccess(ProjectConstant.COMMON_STATUS_AVAILABLE);
//		if(200 == startSendCode.getIntValue("code")) {
//			redisUtil.set(ProjectConstant.RedisKey.MOBILE_CODE.getKeyPrefix() +"_"+ type + "_" + code,mobile,ProjectConstant.RedisKey.MOBILE_CODE.getExpire());
//			smsRecord.setStatus(ProjectConstant.COMMON_STATUS_AVAILABLE);
//			this.save(smsRecord);
//			return AjaxResult.success("发送验证码成功");
//		}
//		this.save(smsRecord);
//		return AjaxResult.error("发送失败");
	}
}
